package com.ruoyi.project.system.task.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.task.domain.Transfer;
import com.ruoyi.project.system.task.service.TransferService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/system/task/tranf")
public class TransferController extends BaseController {

    private String prefix = "system/task/tranf";

    @Autowired
    private TransferService transferService;

    /**
     * 跳转到任务转移
     * @return
     */
    @RequiresPermissions("system:tranf:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/tranf";
    }

    /**
     * 任务转移
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增任务转移
     */
    @RequiresPermissions("system:tranf:add")
    @Log(title = "转移任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Transfer transfer)
    {
        System.out.println(transfer.toString());
        return toAjax(transferService.insertTranf(transfer));
    }

    /**
     * 修改任务转移
     */
    @GetMapping("/edit/{transferId}")
    public String edit(@PathVariable("transferId") Integer transferId, ModelMap mmap)
    {
        mmap.put("tranf", transferService.selectTranfById(transferId));
        return prefix + "/edit";
    }

    /**
     * 修改任务转移
     */
    @RequiresPermissions("system:tranf:edit")
    @Log(title = "任务转移", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Transfer transfer)
    {
        System.out.println(transfer.toString());
        return toAjax(transferService.updateTranfTask(transfer));
    }

    /**
     * 更具条件分页查询
     * @param transfer 条件
     * @return
     */
    @RequiresPermissions("system:tranf:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Transfer transfer)
    {
        startPage();
        List<Transfer> list = transferService.selectTransferList(transfer);
        return getDataTable(list);
    }

    /**
     * 一键导出表
     * @param transfer 查询条件
     * @return
     */
    @Log(title = "任务转移审核", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:tranf:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Transfer transfer)
    {
        List<Transfer> list = transferService.selectTransferList(transfer);
        ExcelUtil<Transfer> util = new ExcelUtil<Transfer>(Transfer.class);
        return util.exportExcel(list, "任务转移数据");
    }

    /**
     * 根据转移任务id查看任务信息
     * @param transferId 延期id
     * @param mmap
     * @return
     */
    @GetMapping("/show/{transferId}")
    public String show(@PathVariable("transferId") Integer transferId, ModelMap mmap)
    {
        mmap.put("tranf", transferService.selectTranfById(transferId));
        return prefix + "/show";
    }

    /**
     * 校验任务id是否存在并且已审核通过
     */
    @PostMapping("/checkTranfTaskIdUnique")
    @ResponseBody
    public String checkTranfTaskIdUnique(Transfer transfer)
    {
        return transferService.checkTranfTaskIdUnique(transfer);
    }

    /**
     * 根据任务转移id删除转移任务
     */
    @RequiresPermissions("system:tranf:remove")
    @Log(title = "任务转移", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(transferService.deleteTranfByIds(ids));
    }
}
